package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.Commodity;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.PurchaseOrder;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.CommodityListReq;
import com.example.xpvideo.entity.req.OrderReq;
import com.example.xpvideo.entity.res.CommodityListRes;
import com.example.xpvideo.entity.res.OrderListRes;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.PurchaseOrderMapper;
import com.example.xpvideo.service.PurchaseOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 采购商订单表(PurchaseOrder)Service层
 *
 * @author 杨丽辉
 * @since 2021-08-09 18:19:28
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class PurchaseOrderServiceImpl implements PurchaseOrderService {
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Override
    public ResponseResult<PageResult<List<OrderListRes>>> list(OrderReq orderReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String merchantsName = orderReq.getMerchantsName();
        String nickname = orderReq.getNickname();
        String orderCode = orderReq.getOrderCode();
        Integer orderType = orderReq.getOrderType();
        Integer orderStage = orderReq.getOrderStage();

        PageHelper.startPage(page, size);

        List<OrderListRes> list = purchaseOrderMapper.OrderList(Wrappers.<PurchaseOrder>query()
                .like(!ObjectUtils.isEmpty(merchantsName),"merchants_name",merchantsName)
                .like(!ObjectUtils.isEmpty(nickname),"nickname",nickname)
                .eq(!ObjectUtils.isEmpty(orderCode), "order_code",orderCode)
                .eq(!ObjectUtils.isEmpty(orderType), "order_type",orderType)
                .eq(!ObjectUtils.isEmpty(orderStage),"order_stage",orderStage));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<OrderListRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

}

